import 'package:flutter/material.dart';

import '../../../../pojo/ImPojo.dart';
import '../../../../util/ImConstant.dart';
import '../../../bloc/MessageListCubit.dart';
import 'MessagePane.dart';

class ChatMessagePane extends StatelessWidget {

  final WrapImMessage wrapMessage;

  const ChatMessagePane({ required this.wrapMessage, super.key});

  @override
  Widget build(BuildContext context) {
    if (wrapMessage.message.messageType != ImMessageType.Chat) {
      throw Exception("消息类型不对${wrapMessage.message.messageType.name}");
    }

    return Padding(
      padding: const EdgeInsets.only(top: 2, right: 5, bottom: 2, left: 5),
      child: Column(children: _buildPanes(),),
    );
  }

  Widget _buildContentTitle() {
    if (wrapMessage.message.isGroup) {
      return Text('群聊的聊天信息', style: const TextStyle(fontSize: ImConstant.IM_DEFAULT_SIZE, fontWeight: FontWeight.w400));
    }
    return Text("${wrapMessage.message.fromImUser?.userName??''}",
        style: const TextStyle(fontSize: ImConstant.IM_DEFAULT_SIZE, fontWeight: FontWeight.w400));
  }

  Widget _buildBottom() {
    return Text('聊天记录');
  }

  List<Widget> _buildPanes() {
    var result = <Widget>[];
    result.add(_buildContentTitle());
    var contents = wrapMessage.message.refMessages!.map((refMessage){
        return MessageRefPane(message: refMessage, param: MessageRefParam(maxLines: 1),);
    });
    result.addAll(contents);
    result.add(_buildBottom());
    return result;
  }
}

class ChatMessageRefPane extends StatelessWidget {

  final ImMessage message;
  final MessageRefParam param;

  const ChatMessageRefPane({ required this.message, required this.param, super.key});

  @override
  Widget build(BuildContext context) {
    if (message.messageType != ImMessageType.Chat) {
      throw Exception("消息类型不对${message.messageType.name}");
    }

    return Padding(
      padding: const EdgeInsets.only(top: 2, right: 5, bottom: 2, left: 5),
      child: Column(children: _buildPanes(),),
    );
  }

  Widget _buildContentTitle() {
    if (message.isGroup) {
      return Text('群聊的聊天信息', style: TextStyle(fontSize:  param.fontSize, fontWeight: FontWeight.w400));
    }
    return Text("${message.fromImUser?.userName??''}",
        style: TextStyle(fontSize: param.fontSize, fontWeight: FontWeight.w400));
  }

  Widget _buildBottom() {
    return const Text('聊天记录');
  }

  List<Widget> _buildPanes() {
    var result = <Widget>[];
    result.add(_buildContentTitle());
    var contents = message.refMessages!.map((refMessage){
      return MessageRefPane(message: refMessage, param: param,);
    });
    result.addAll(contents);
    result.add(_buildBottom());
    return result;
  }
}